﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DaoDataEntity;
using MySql.Data.MySqlClient;
using DBAccess;
using LOG;

namespace CacheAccess
{
   public class EquipManager
    {
       public EquipinfoEntity getEquipByID(int equipID)
       {
           if (CacheManager.m_Equipinfo.ContainsKey(equipID))
           {
               EquipinfoEntity ret = CacheManager.m_Equipinfo[equipID];
               if (CacheManager.m_Equipbaseinfo.ContainsKey(ret.EquipBaseInfoID))
               {
                   ret.Equipbase = CacheManager.m_Equipbaseinfo[ret.EquipBaseInfoID];
                   return ret;
               }
               else
                   return null;

           }
           else
               return null;
       }

       public bool dressEquip(string part,BackageEntity backage,int equipID,int playerID)
       {
           //最后更新cache
           MasterinfoEntity master = CacheManager.m_Masterinfo[playerID];
           Common com = new Common();
           MySqlCommand cmd = new MySqlCommand();
           MySqlConnection con = com.getConnection();
           con.Open();
           MySqlTransaction trans = con.BeginTransaction();
           BackageEntity oldEquip = new BackageEntity();
           try
           {
               cmd.Connection = trans.Connection;
               cmd.Transaction = trans;
               int oldEquipID = -1;
               switch (part)
               {
                   case "Headpiece":
                       if (CacheManager.m_Masterinfo[playerID].Headpiece > 0)
                       {
                           //先把换下来的装备放进背包
                           cmd.CommandText = "insert into";
                           com.executeCommandTran(con, cmd, ref oldEquipID);
                       }
                       else
                           oldEquipID = 0;
                       break;
                   case "Army":
                       if (CacheManager.m_Masterinfo[playerID].Army > 0)
                       {
                           //先把换下来的装备放进背包
                           cmd.CommandText = "insert into";
                           com.executeCommandTran(con, cmd, ref oldEquipID);
                       }
                       else
                           oldEquipID = 0;
                       break;
                   case "Clouth":
                       if (CacheManager.m_Masterinfo[playerID].Clouth > 0)
                       {
                           //先把换下来的装备放进背包
                           cmd.CommandText = "insert into";
                           com.executeCommandTran(con, cmd, ref oldEquipID);
                       }
                       else
                           oldEquipID = 0;
                       break;
                   case "Shoes":
                       if (CacheManager.m_Masterinfo[playerID].Shoes > 0)
                       {
                           //先把换下来的装备放进背包
                           cmd.CommandText = "insert into";
                           com.executeCommandTran(con, cmd, ref oldEquipID);
                       }
                       else
                           oldEquipID = 0;
                       break;
                   case "Jewelry":
                       if (CacheManager.m_Masterinfo[playerID].Jewelry > 0)
                       {
                           //先把换下来的装备放进背包
                           cmd.CommandText = "insert into";
                           com.executeCommandTran(con, cmd, ref oldEquipID);
                       }
                       else
                           oldEquipID = 0;
                       break;
                   default:
                       break;
               }

               //先更新master表
               cmd.CommandText = "update 1 ";
               cmd.Parameters.Clear();
               int masterid = -1;//不用
               int baseinfoID = -1;//不用

                com.executeCommandTran(con, cmd, ref masterid);
               //再更新backage表
               cmd.CommandText = "delete 2";
               cmd.Parameters.Clear();
                com.executeCommandTran(con, cmd, ref baseinfoID);
                BackageManager b = new BackageManager();
               if (masterid>=0 && baseinfoID>=0&&oldEquipID>=0)
               {
                   trans.Commit();
                   if (oldEquipID > 0)
                   {
                       oldEquip.BackageID = oldEquipID;
                       oldEquip.GoodsID = CacheManager.m_Masterinfo[playerID].Headpiece;
                       oldEquip.GoodsType = "equipinfo";
                       oldEquip.MasterID = playerID;
                       oldEquip.PlatformSign = CacheManager.m_Masterinfo[playerID].PlatformSign;
                       //向背包中添加一件物品
                      
                       if (!b.addBackage(oldEquip, playerID))
                       {
                           Log.Add("向缓存backage中添加物品失败"+oldEquipID, -791);
                           return false;
                       }
                   }
                   if (CacheManager.m_Masterinfo.ContainsKey(playerID) && CacheManager.m_Backage.ContainsKey(playerID))
                   {
                       switch (part)
                       {
                           case "Headpiece":
                               CacheManager.m_Masterinfo[playerID].Headpiece = equipID;
                               break;
                           case "Army":
                               CacheManager.m_Masterinfo[playerID].Army = equipID;
                               break;
                           case "Clouth":
                               CacheManager.m_Masterinfo[playerID].Clouth = equipID;
                               break;
                           case "Shoes":
                               CacheManager.m_Masterinfo[playerID].Shoes = equipID;
                               break;
                           case "Jewelry":
                               CacheManager.m_Masterinfo[playerID].Jewelry = equipID;
                               break;
                           default:
                               break;
                       }
                       if (!b.delBackage(backage.BackageID, playerID))
                           return false;
                       return true;
                   }
                   else
                   {
                       Log.Add("缓存中未找到backage或master" + backage.BackageID.ToString() + " master" + backage.MasterID.ToString(), -792);
                       return false;
                   }
               }
               else
                   trans.Rollback();
               return false;
           }
           catch (Exception ex)
           {
               trans.Rollback();
               Log.Add(ex.ToString(), -794);
               return false;
           }
           finally
           {
               con.Close();
               con.Dispose();
               cmd.Dispose();
           }
       }
      
    }
}
